﻿using MediatR;
using Microsoft.AspNetCore.Mvc;
using SupplyChain.Domain.Entities;
using SupplyChain.ErrorCode;
using SupplyChain.Write.API.Application.Command.Goods;

namespace SupplyChain.Write.API.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class GoodsController : ControllerBase
    {
        private readonly IMediator mediator;

        public GoodsController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 新增产品
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResponse<bool>> AddGoods(AddGoodsCommand request)
        {
            bool result = await mediator.Send(request);
            if (result)
            {
                return ApiResponse<bool>.Success(result);
            }
            return ApiResponse<bool>.Error(400, "操作失败");
        }

        /// <summary>
        /// 查询产品
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResponse<List<GoodsInfo>>> GetGoods()
        {
            return ApiResponse<List<GoodsInfo>>.Success(await mediator.Send(new GetGoodsCommand()));
        }

        /// <summary>
        /// 删除产品
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ApiResponse<bool>> DeleteGoods([FromQuery] DelGoodsCommand command)
        {
            bool result = await mediator.Send(command);
            if (result)
            {
                return ApiResponse<bool>.Success(result);
            }
            return ApiResponse<bool>.Error(400, "操作失败");
        }
    }
}
